﻿using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using XH.SmartParking.IService;
using XH.SmartParking.Entities;
using XH.SmartParking.Models;

namespace XH.SmartParking.ViewModels.Pages.Dialogs
{
    public class ModifyRechargeViewModel : DialogViewModelBase
    {
        public RechargeModel RechargeInfo { get; set; } =
            new RechargeModel();

        public List<string> AutoList { get; set; } = new List<string>();
        public List<BaseFeeMode> FeeModeList { get; set; } = new List<BaseFeeMode>();

        int _userId;
        string _userName;
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            Title = "会员续费登记";
            RechargeInfo.RechargeEndTime = DateTime.Now;

            var user = parameters.GetValue<UserModel>("user");
            if (user != null)
            {
                _userId = user.UserId;
                _userName = user.UserName;
            }
        }

        public override void DoSave()
        {
            try
            {
                _rechargeService.Insert(new MemberRecharge
                {
                    AutoLisence = RechargeInfo.AutoLisence,
                    FeeModeId = RechargeInfo.FeeModeId,
                    RechargeCount = RechargeInfo.RechargeCount,
                    RechargeEndTime = RechargeInfo.RechargeEndTime.ToString(),
                    State = 1,
                    CreateTime = DateTime.Now.ToString(),
                    CreateId = _userId,
                    CreateName = _userName
                });



                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

        IRechargeService _rechargeService;
        public ModifyRechargeViewModel(IRechargeService rechargeService)
        {
            _rechargeService = rechargeService;

            AutoList = _rechargeService.Query<AutoRegister>(q => q.State == 1).Select(a => a.AutoLicense).ToList();
            FeeModeList = _rechargeService.Query<BaseFeeMode>(q => true).ToList();
        }
    }
}
